package com.zcareu.logServ.controller;


import com.zcareu.logServ.service.LogServ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class LogController {
    @Autowired
    private LogServ logServ;

    @GetMapping(value = "/log")
    public List index(
            @RequestParam(required = false) String filterName, @RequestParam(required = false) String query,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date startWith,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date endWith,
            @RequestParam(required = false) Integer offset, @RequestParam(required = false) Integer line) {

        if(startWith == null)
            startWith = new Date(new Date().getTime() - 86400000);
        if(endWith == null)
            endWith = new Date();
        if(query == null)
            query = "";
        if(offset == null)
            offset = 0;
        if(line == null)
            line = 10;

        return logServ.getLogs(filterName, query, startWith, endWith, line, offset);
    }
}
